/******************************************************************************
 *  author: enquanyan
 *  version: 1.0
 *  description: link queue without lock
 *  date: 2014-07-24
 *  other: 无
 ******************************************************************************/
 
#ifndef LINK_QUEUE_H
#define LINK_QUEUE_H

template<typename element_type>
struct qnode
{
	struct qnode *next;
	element_type data;
};

template<typename element_type>
class link_queue
{
public:
	link_queue()
	{
		init();
	}
	
	~link_queue() 
	{
		destory();
	}
	
	bool init();
	
	void destory();
	
	bool push(element_type e);
	
	bool pop(element_type *e);
	
private:
	struct qnode<element_type> * volatile m_front, * volatile m_rear;
};

template<typename element_type>
bool link_queue<element_type>::init()
{
		m_front = m_rear = new qnode<element_type>;
		if (!m_front)
			return false;
		m_front->next = NULL;
		return true;
}

template<typename element_type>
void link_queue<element_type>::destory()
{
		while (m_front)
		{
			m_rear = m_front->next;
			delete m_front;
			m_front = m_rear;
		}
}

template<typename element_type>
bool link_queue<element_type>::push(element_type e)
{
		//printf("into link_queue push\n");
		struct qnode<element_type> *p = new qnode<element_type>;
		if (!p)
			return false;
		p->next = NULL;
		m_rear->next = p;
		m_rear->data = e;
		m_rear = p;
		//printf("into link_queue end\n");
		return true;
}

template<typename element_type>
bool link_queue<element_type>::pop(element_type *e)
{
		if (m_front == m_rear)
			return false;
	
		struct qnode<element_type> *p = m_front;		
		*e = p->data;
		m_front = p->next;
		delete p;
		return true;
}


#endif 